package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.Vo.DoctorAssessmentVo;
import com.khyr.Vo.InsertCommentVo;

import com.khyr.dao.DoctorAssessmentDao;
import com.khyr.dao.HisCareHistoryDao;
import com.khyr.dao.HisPatientDao;
import com.khyr.dao.UserDao;
import com.khyr.entity.Comment;
import com.khyr.entity.HisCareHistory;
import com.khyr.entity.HisPatient;
import com.khyr.entity.User;
import com.khyr.service.DoctorAssessmentService;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class DoctorAssessmentServiceImpl implements DoctorAssessmentService {
    @Autowired
    private HisPatientDao hisPatientDao;
    @Autowired
    private HisCareHistoryDao hisCareHistoryDao;
    @Autowired
    private DoctorAssessmentDao doctorAssessmentDao;
    @Autowired
    private UserDao userDao;



    @Override
    public R<Page<Comment>> queryAllNew(DoctorAssessmentVo doctorAssessmentVo) {
        //分页
        Page<Comment> Page = new Page<>(doctorAssessmentVo.getPageNum(), doctorAssessmentVo.getPageSize());
        //模糊
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(doctorAssessmentVo.getName())){
            QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.like("user_name", doctorAssessmentVo.getName());
            List<User> user = userDao.selectList(queryWrapper2);
            //创建一个list
            List<Integer> list = new ArrayList<>();
            //遍历得到id
            for (User comment1 : user){
                // 获取评论者的id
                Long ValueId = comment1.getUserId();
                //转换成int
                list.add(ValueId.intValue());
            }
            System.out.println(list);
            queryWrapper.in("value_id", list);
        }
        if (StringUtils.hasText(doctorAssessmentVo.getBeginTime())&& StringUtils.hasText(doctorAssessmentVo.getEndTime())){
            queryWrapper.between("add_time", doctorAssessmentVo.getBeginTime(), doctorAssessmentVo.getEndTime());
        }
        Page<Comment> PageInfo = doctorAssessmentDao.selectPage(Page, queryWrapper);
        if (PageInfo != null){
            return new R<>(200,"查询成功", PageInfo);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<Integer> deleteNewByIds(List<Long> ids) {
        for (Long id : ids){
            int i = doctorAssessmentDao.deleteById(id);
            if (i > 0){
                return new R<>(200,"删除成功", i);
            }
        }
        return new R<>(500,"删除失败", null);
    }
    /**
     * @create by: Zr
     * @description: 根据医生id查询评论
     * @create time: 2025/3/19 15:38
     * @param id
     * @return com.khyr.vo.R<java.util.List<com.khyr.entity.Comment>>
     */
    @Override
    public R<List<Comment>> getUserCommentById(Long id) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("value_id",id);
        List<Comment> list = doctorAssessmentDao.selectList(queryWrapper);
        for(Comment comment : list){
            // 查询每个评论者的头像和昵称
            QueryWrapper<HisPatient> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("patient_id",comment.getParentId());
            HisPatient hisPatient = hisPatientDao.selectOne(queryWrapper1);
            comment.setAvatar(hisPatient.getAvatar());
            comment.setName(hisPatient.getName());

        }
        if (list != null){
            return new R<>(200,"查询成功", list);
        }else {
            return new R<>(500,"查询失败", null);
        }
    }

    @Override
    public R<String> insertComment(InsertCommentVo insertCommentVo, HisPatient patient) {
        Comment comment = new Comment();
        // 通过病例id查询医生id
        QueryWrapper<HisCareHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ch_id",insertCommentVo.getChId());
        HisCareHistory hisCareHistory = hisCareHistoryDao.selectOne(queryWrapper);
        if (hisCareHistory != null){
            // 设置医生id
            comment.setValueId(Math.toIntExact(hisCareHistory.getUserId()));
        }
        // 设置评论类型，默认为0，即患者对医生的评论
        comment.setType(0);
        // 判断用户是否对医生进行评论，如果有评论内容，则设置评论内容，否则设置默认评论内容
        if(insertCommentVo.getContent() != null){
            comment.setContent(insertCommentVo.getContent());
        }else {
            comment.setContent("该用户没有评价，仅对该医生打分");
        }
        // 设置评论者id
        comment.setParentId(patient.getPatientId());
        // 设置是否有图片，由于没有前端图片上传默认为0，即没有图片
        comment.setHasPicture(0);
        // 设置图片路径，由于没有前端图片上传默认为空
        comment.setPicUrls(null);
        // 设置评分,其不会为空，因此不做判断
        comment.setStar(insertCommentVo.getStar());
        // 设置创建时间
        comment.setAddTime(new Date());
        // 设置更新时间
        comment.setUpdateTime(new Date());
        // 设置逻辑删除，默认为0，即未删除
        comment.setDeleted(0);
        int insert = doctorAssessmentDao.insert(comment);
        if (insert > 0){
            return new R<>(200,"评论成功", "评论成功");
        }else {
            return new R<>(500,"评论失败", "评论失败");
        }

    }
}
